Copyright (C) 2003,2004 dr. Cristiano Sadun

org.sadun.text.ffp
Class LineFormat

java.lang.Object
  extended byorg.sadun.text.ffp.LineFormat
All Implemented Interfaces:
java.lang.Cloneable

public class LineFormat
extends java.lang.Object
implements java.lang.Cloneable

An ojbect of this class holds details of a specific logical line format and can parse it from core memory. A logical line can be composed by several consecutive physical lines, separated by a line separator character sequence.

A LineFormat is built by declaring the expected fields and used by invoking the parse(String) method. Three ways are available to declare the expected fields:

For each method, overloads which accept a physical line number as parameter or not are provided: if the parameter is absent, the physical line number is intended 1 (the common case of parsing a single line).

A LineFormat can be copied in whichever moment, and the resulting copy is independent from the original object. This allows to minimize the amount of declarations necessary for creating LineFormats for similar lines.

When parse(String) is invoked, the object attempts to parse the passed text according to the format definition. If the parsing fails, a FFPParseException subexception is thrown. If the line is parsed correctly but some characters are still existing before the line separator:

isFailOnTrailingChars() is initialized on construction depending on the value of the public static field defaultFailOnTrailingChars, which is by default set to true (i.e. indicating that an exception should be raised on trailing characters).

Version:
1.0
Author:
Cristiano Sadun

Nested Class Summary
 class LineFormat.FieldInfo
          A class describing a field.
 
Field Summary
static boolean defaultFailOnTrailingChars
          This member controls the default value of the failOnTrailingChars property.
 
Constructor Summary
LineFormat()
          Create a LineFormat, for reading a logical line.
LineFormat(java.lang.String name)
          Create a LineFormat with a name, for reading a logical line.
 
Method Summary
 LineFormat createCopy()
          Create an independent copy of the format.
 void declareLineImage(int physicalLine, java.lang.String image)
          This method allows to declare quickly a line format by providing an image.
 void declareLineImage(java.lang.String image)
          This method allows to declare quickly a line format by providing an image.
 void defineField(int start, int end)
          Define a field as being at the given start-end position.
 void defineField(int physicalLine, int start, int end)
          Define a field as being in the given physical line and at the given start-end position.
 void defineField(int physicalLine, int start, int end, Type type)
          Define a field as being in the given physical line and at the given start-end position.
 void defineField(int physicalLine, int start, int end, Type type, java.lang.String image)
          Define a field as being in the given physical line and at the given start-end position.
 void defineField(int start, int end, Type type)
          Define a field as being at the given start-end position and having the given type.
 void defineField(int physicalLine, java.lang.String name, int start, int end)
          Define a field as being in the given physical line and at the given start-end position.
 void defineField(int physicalLine, java.lang.String name, int start, int end, Type type)
          Define a field as being in the given physical line and at the given start-end position.
 void defineField(int physicalLine, java.lang.String name, int start, int end, Type type, java.lang.String image)
          Define a field as being in the given physical line and at the given start-end position.
 void defineField(java.lang.String name, int start, int end)
          Define a field as being at the given start-end position and having the given name.
 void defineField(java.lang.String name, int start, int end, Type type)
          Define a field as being at the given start-end position and having the given name and type.
 void defineFields(int[][] positions)
          Define fields using a positions array.
 void defineFields(int physicalLine, int[][] positions)
          Define fields in the given physical line using a positions array.
 void defineNextField(int end)
          Define a field as being located from the current start position to the given end position (excluded).
 void defineNextField(int physicalLine, int end)
          Define a field as being in the given physical line, from the current start position to the given end position (excluded).
 void defineNextField(int physicalLine, int end, Type type)
          Define a field as being in the given physical line, having the given type and being located from the current start position to the given end position (excluded).
 void defineNextField(int physicalLine, int end, Type type, java.lang.String image)
          Define a field as being in the given physical line, having the given type and image and being located from the current start position to the given end position (excluded).
 void defineNextField(int physicalLine, java.lang.String name, int end)
          Define a field as being in the given physical line, having the given name and being located from the current start position to the given end position (excluded).
 void defineNextField(int physicalLine, java.lang.String name, int end, Type type)
          Define a field as being in the given physical line, having the given name and type and being located from the current start position to the given end position (excluded).
 void defineNextField(int physicalLine, java.lang.String name, int end, Type type, java.lang.String image)
          Define a field as being in the given physical line, having the given name, type and image and being located from the current start position to the given end position (excluded).
 void defineNextField(int end, Type type)
          Define a field as having the given type and being located from the current start position to the given end position (excluded).
 void defineNextField(int end, Type type, java.lang.String image)
          Define a field as having the given type and image and being located from the current start position to the given end position (excluded).
 void defineNextField(java.lang.String name, int end)
          Define a field as having the given name and being located from the current start position to the given end position (excluded).
 void defineNextField(java.lang.String name, int end, Type type)
          Define a field as having the given name and being located from the current start position to the given end position (excluded), and given type
 void defineNextField(java.lang.String name, int end, Type type, java.lang.String image)
          Define a field as having the given name and being located from the current start position to the given end position (excluded), and given type and image
 void defineNextFields(int[] positions)
          Define consecutive fields using a positions array.
 void defineNextFields(int physicalLine, int[] positions)
          Define consecutive fields in the given physical line using a positions array.
static java.lang.String extract(java.lang.CharSequence line, int start, int end)
          An utility method to extracts a portion of a physical line.
 java.lang.String formatValuesAsXML(int lineCount, java.lang.String[] values)
          An utility method to format a set of values to an XML format, according to this LineFormat.
 java.lang.String formatValuesAsXML(java.lang.String[] values)
          An utility method to format a set of values to an XML format, according to this LineFormat.
static LineFormat fromImage(java.lang.String image)
          Allows to create a LineFormat object directly from an image.
static LineFormat fromImage(java.lang.String name, java.lang.String image)
          Allows to create a named LineFormat object directly from an image.
 int getFieldsCount()
          Return the number of fields in the logical line.
 int getFieldsCount(int physicalLine)
          Return the number of fields in the physical line.
 java.lang.String getLineSeparator()
          Return the line separator.
 java.lang.String getName()
          Return the name of this line format.
 int getPhysicalLinesCount()
          Return the physical lines count at the moment of call.
 boolean isFailOnTrailingChars()
          Return whether or not parsing must fails if a line is parsed correctly but not all its characters are consumed.
 java.util.Iterator iterator()
          Return an iterator on this format's LineFormat.FieldInfo set.
static java.lang.String makeConstantImage(java.lang.String s)
          Return a string constant translated to the image syntax recognized by this class.
 java.lang.String[] parse(java.lang.String s)
          Parse the given line basing on the format.
 java.lang.String[] parse(java.lang.String s, boolean autoTrim)
          Parse the given line basing on the format.
 java.lang.String parseToXml(java.lang.String s)
          An utility method, which parses a line, and returns its XML fragment representation.
 java.lang.String parseToXml(java.lang.String s, boolean autoTrim)
          An utility method, which parses a line, and returns its XML fragment representation.
 void setFailOnTrailingChars(boolean failOnTrailingChars)
          Set whether or not parsing must fails if a line is parsed correctly but not all its characters are consumed.
 void setLineSeparator(java.lang.String lineSeparator)
          Set the line separator.
 java.lang.String toString()
          Return a human-readable description of the format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultFailOnTrailingChars

public static boolean defaultFailOnTrailingChars
This member controls the default value of the failOnTrailingChars property. It is set to true when the class is loaded.

Constructor Detail

LineFormat

public LineFormat(java.lang.String name)
Create a LineFormat with a name, for reading a logical line.

A logical line may be composed by multiple physical lines separated by a line separator.


LineFormat

public LineFormat()
Create a LineFormat, for reading a logical line.

A logical line may be composed by multiple physical lines separated by a line separator.

Method Detail

getName

public java.lang.String getName()
Return the name of this line format.

Returns:
the name of this line format.

getLineSeparator

public java.lang.String getLineSeparator()
Return the line separator. By default, the line separator is the value of the line.separator system property.

Returns:
Returns the lineSeparator.

setLineSeparator

public void setLineSeparator(java.lang.String lineSeparator)
Set the line separator. By default, the line separator is the value of the line.separator system property.

Parameters:
lineSeparator - The lineSeparator to set.

getPhysicalLinesCount

public int getPhysicalLinesCount()
Return the physical lines count at the moment of call.

Note that if defineField(int, int, int)is invoked declaring a field into a greater physical line, the count returned by this method will adjust accordingly.

Returns:
the physical lines count at the moment of call.

extract

public static java.lang.String extract(java.lang.CharSequence line,
                                       int start,
                                       int end)
                                throws IllegalRangeException
An utility method to extracts a portion of a physical line.

Parameters:
line - the character array containing the physical line
start - the start index
end - the end index
Returns:
the String comprised between start and end (excluded).
Throws:
IllegalRangeException - if the given range is not valid

defineFields

public void defineFields(int physicalLine,
                         int[][] positions)
Define fields in the given physical line using a positions array. As many fields as elements in the array are defined. Each element must be a two-integer array containing start and end position of the field (excluded).

Parameters:
positions - as many fields as elements in the array are defined. Each element of the array must be a two-integer array containing start and end position of the field (excluded).

defineFields

public void defineFields(int[][] positions)
Define fields using a positions array. As many fields as elements in the array are defined. Each element must be a two-integer array containing start and end position of the field (excluded).

Parameters:
positions - as many fields as elements in the array are defined. Each element of the array must be a two-integer array containing start and end position of the field (excluded).

defineField

public void defineField(int physicalLine,
                        int start,
                        int end)
Define a field as being in the given physical line and at the given start-end position. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Physical lines indexes start at 1. Start and end indexes start at 0.

Note: if the given physical line is greater than the current physical line, the latter is increased; the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
physicalLine - the physical line index (starting from 1)
start - the start index in the line
end - the end index in the line

defineField

public void defineField(int physicalLine,
                        int start,
                        int end,
                        Type type)
Define a field as being in the given physical line and at the given start-end position. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Physical lines indexes start at 1. Start and end indexes start at 0.

Note: if the given physical line is greater than the current physical line, the latter is increased; the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
physicalLine - the physical line index (starting from 1)
start - the start index in the line
end - the end index in the line
type - the expected type of the parameter

defineField

public void defineField(int physicalLine,
                        int start,
                        int end,
                        Type type,
                        java.lang.String image)
Define a field as being in the given physical line and at the given start-end position. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Physical lines indexes start at 1. Start and end indexes start at 0.

Note: if the given physical line is greater than the current physical line, the latter is increased; the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
physicalLine - the physical line index (starting from 1)
start - the start index in the line
end - the end index in the line
type - the expected type of the parameter
image - the image of the element

defineField

public void defineField(int physicalLine,
                        java.lang.String name,
                        int start,
                        int end)
Define a field as being in the given physical line and at the given start-end position. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Physical lines indexes start at 1. Start and end indexes start at 0.

Note: if the given physical line is greater than the current physical line, the latter is increased; the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
physicalLine - the physical line index (starting from 1)
name - the name of the field
start - the start index in the line
end - the end index in the line

defineField

public void defineField(int physicalLine,
                        java.lang.String name,
                        int start,
                        int end,
                        Type type)
Define a field as being in the given physical line and at the given start-end position. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Physical lines indexes start at 1. Start and end indexes start at 0.

Note: if the given physical line is greater than the current physical line, the latter is increased; the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
physicalLine - the physical line index (starting from 1)
name - the name of the field
start - the start index in the line
end - the end index in the line
type - the expected type of the parameter

defineField

public void defineField(int physicalLine,
                        java.lang.String name,
                        int start,
                        int end,
                        Type type,
                        java.lang.String image)
Define a field as being in the given physical line and at the given start-end position. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Physical lines indexes start at 1. Start and end indexes start at 0.

Note: if the given physical line is greater than the current physical line, the latter is increased; the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

This is the method allowing the most complete field definition.

Parameters:
physicalLine - the physical line index (starting from 1)
name - the name of the field
start - the start index in the line
end - the end index in the line
type - the expected type of the parameter
image - the image of the element

defineField

public void defineField(int start,
                        int end)
Define a field as being at the given start-end position. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Note: the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
start - the start index in the line
end - the end index in the line

defineField

public void defineField(int start,
                        int end,
                        Type type)
Define a field as being at the given start-end position and having the given type. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Note: the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
start - the start index in the line
end - the end index in the line
type - the expected type of the parameter

defineField

public void defineField(java.lang.String name,
                        int start,
                        int end)
Define a field as being at the given start-end position and having the given name. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Note: the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
name - the name of the field
start - the start index in the line
end - the end index in the line

defineField

public void defineField(java.lang.String name,
                        int start,
                        int end,
                        Type type)
Define a field as being at the given start-end position and having the given name and type. The end position is not included, so a positional field at [0, 10] inclusive will have start=0 and end=11;

Note: the current start position is advanced to the given end position (for usage with defineNextField(int, int)).

Parameters:
name - the name of the field
start - the start index in the line
end - the end index in the line
type - the expected type of the parameter

defineNextFields

public void defineNextFields(int physicalLine,
                             int[] positions)
Define consecutive fields in the given physical line using a positions array. As many fields as elements in the array are defined. Each element indicates the end position of the field (excluded).

Parameters:
positions - as many fields as elements in the array are defined. Each element indicates the end position of the field (excluded).

defineNextFields

public void defineNextFields(int[] positions)
Define consecutive fields using a positions array. As many fields as elements in the array are defined. Each element indicates the end position of the field (excluded).

Parameters:
positions - as many fields as elements in the array are defined. Each element indicates the end position of the field (excluded).

defineNextField

public void defineNextField(int physicalLine,
                            int end)
Define a field as being in the given physical line, from the current start position to the given end position (excluded).

Parameters:
physicalLine - the physical line index (starting from 1)
end - the end position (excluded).

defineNextField

public void defineNextField(int physicalLine,
                            int end,
                            Type type)
Define a field as being in the given physical line, having the given type and being located from the current start position to the given end position (excluded).

Parameters:
physicalLine - the physical line index (starting from 1)
end - the end position (excluded).
type - the expected type of the parameter

defineNextField

public void defineNextField(int physicalLine,
                            int end,
                            Type type,
                            java.lang.String image)
Define a field as being in the given physical line, having the given type and image and being located from the current start position to the given end position (excluded).

Parameters:
physicalLine - the physical line index (starting from 1)
end - the end position (excluded).
type - the expected type of the parameter
image - the image of the parameter

defineNextField

public void defineNextField(int physicalLine,
                            java.lang.String name,
                            int end)
Define a field as being in the given physical line, having the given name and being located from the current start position to the given end position (excluded).

Parameters:
name - the name of the field
physicalLine - the physical line index (starting from 1)
end - the end position (excluded).

defineNextField

public void defineNextField(int physicalLine,
                            java.lang.String name,
                            int end,
                            Type type)
Define a field as being in the given physical line, having the given name and type and being located from the current start position to the given end position (excluded).

Parameters:
name - the name of the field
physicalLine - the physical line index (starting from 1)
end - the end position (excluded).
type - the expected type of the parameter

defineNextField

public void defineNextField(int physicalLine,
                            java.lang.String name,
                            int end,
                            Type type,
                            java.lang.String image)
Define a field as being in the given physical line, having the given name, type and image and being located from the current start position to the given end position (excluded).

Parameters:
name - the name of the field
physicalLine - the physical line index (starting from 1)
end - the end position (excluded).
type - the expected type of the parameter
image - the image of the parameter

defineNextField

public void defineNextField(int end)
Define a field as being located from the current start position to the given end position (excluded).

Parameters:
end - the end position (excluded).

defineNextField

public void defineNextField(java.lang.String name,
                            int end)
Define a field as having the given name and being located from the current start position to the given end position (excluded).

Parameters:
name - the name of the field
end - the end position (excluded).

defineNextField

public void defineNextField(java.lang.String name,
                            int end,
                            Type type)
Define a field as having the given name and being located from the current start position to the given end position (excluded), and given type

Parameters:
name - the name of the field
end - the end position (excluded)
type - the expected type of the parameter

defineNextField

public void defineNextField(java.lang.String name,
                            int end,
                            Type type,
                            java.lang.String image)
Define a field as having the given name and being located from the current start position to the given end position (excluded), and given type and image

Parameters:
name - the name of the field
end - the end position (excluded).
type - the expected type of the parameter
image - the image of the parameter

defineNextField

public void defineNextField(int end,
                            Type type)
Define a field as having the given type and being located from the current start position to the given end position (excluded).

Parameters:
end - the end position (excluded).*
type - the expected type of the parameter

defineNextField

public void defineNextField(int end,
                            Type type,
                            java.lang.String image)
Define a field as having the given type and image and being located from the current start position to the given end position (excluded).

Parameters:
end - the end position (excluded).*
type - the expected type of the parameter
image - the image of the parameter

createCopy

public LineFormat createCopy()
Create an independent copy of the format.

The copy so obtained can be modifed independetly from the original, for example to parse two slightly different formats.

Returns:
an independent copy of the format.

parse

public java.lang.String[] parse(java.lang.String s)
                         throws FFPParseException
Parse the given line basing on the format. If isFailOnTrailingChars() is true, the match must be total. Else, trailing characters are silently ignored.

Parameters:
s - a string containing the line's physical lines. The last line may or may not have a line separator.
Returns:
an array containing the string representation of all the fields in the line.
Throws:
FFPParseException - if a problem occurs when parsing

parse

public java.lang.String[] parse(java.lang.String s,
                                boolean autoTrim)
                         throws FFPParseException
Parse the given line basing on the format. If isFailOnTrailingChars() is true, the match must be total. Else, trailing characters are silently ignored.

Basing on the value of the autoTrim parameter, values are automatically trimmed before being returned.

Parameters:
s - a string containing the line's physical lines. The last line may or may not have a line separator.
autoTrim - if true strings are trimmed before being returned
Returns:
an array containing the string representation of all the fields in the line.
Throws:
FFPParseException - if a problem occurs when parsing

parseToXml

public java.lang.String parseToXml(java.lang.String s)
                            throws FFPParseException
An utility method, which parses a line, and returns its XML fragment representation.

Parameters:
s - a string containing the line's physical lines. The last line may or may not have a line separator.
Returns:
an array containing the string representation of all the fields in the line.
Throws:
FFPParseException - if a problem occurs when parsing

parseToXml

public java.lang.String parseToXml(java.lang.String s,
                                   boolean autoTrim)
                            throws FFPParseException
An utility method, which parses a line, and returns its XML fragment representation.

Parameters:
s - a string containing the line's physical lines. The last line may or may not have a line separator.
autoTrim - if true strings are trimmed before being returned
Returns:
an array containing the string representation of all the fields in the line.
Throws:
FFPParseException - if a problem occurs when parsing

formatValuesAsXML

public java.lang.String formatValuesAsXML(int lineCount,
                                          java.lang.String[] values)
An utility method to format a set of values to an XML format, according to this LineFormat.

Parameters:
values - the values to format to XML
Returns:
an XML fragment containing the values

formatValuesAsXML

public java.lang.String formatValuesAsXML(java.lang.String[] values)
An utility method to format a set of values to an XML format, according to this LineFormat.

Parameters:
values - the values to format to XML
Returns:
an XML fragment containing the values

toString

public java.lang.String toString()
Return a human-readable description of the format.

Returns:
a human-readable description of the format.

declareLineImage

public void declareLineImage(java.lang.String image)
                      throws ImageParseException
This method allows to declare quickly a line format by providing an image.

The image symbols indicate the type of the field; fields are separated by a space.

Parameters:
image -
Throws:
ImageParseException

declareLineImage

public void declareLineImage(int physicalLine,
                             java.lang.String image)
                      throws ImageParseException
This method allows to declare quickly a line format by providing an image.

The image symbols indicate the type of the field; fields are separated by a space.

Parameters:
physicalLine -
image -
Throws:
ImageParseException

fromImage

public static LineFormat fromImage(java.lang.String image)
                            throws ImageParseException
Allows to create a LineFormat object directly from an image. (see declareLineImage(String)).

Parameters:
image - the format image
Returns:
a LineFormat object to parse that image.
Throws:
ImageParseException - if the image syntax is incorrect.

fromImage

public static LineFormat fromImage(java.lang.String name,
                                   java.lang.String image)
                            throws ImageParseException
Allows to create a named LineFormat object directly from an image. (see declareLineImage(String)).

Parameters:
image - the format image
Returns:
a LineFormat object to parse that image.
Throws:
ImageParseException - if the image syntax is incorrect.

isFailOnTrailingChars

public boolean isFailOnTrailingChars()
Return whether or not parsing must fails if a line is parsed correctly but not all its characters are consumed.

Returns:
true if a line is parsed correctly but not all its characters are consumed, false otherwise.

setFailOnTrailingChars

public void setFailOnTrailingChars(boolean failOnTrailingChars)
Set whether or not parsing must fails if a line is parsed correctly but not all its characters are consumed.

The initial value is controlled by the defaultFailOnTrailingChars static member.

Parameters:
failOnTrailingChars - true if a line which parsed correctly, but whose characters are not totally consumed must make the parser fail.

getFieldsCount

public int getFieldsCount()
Return the number of fields in the logical line.

Returns:
the number of fields in the logical line.

getFieldsCount

public int getFieldsCount(int physicalLine)
Return the number of fields in the physical line.

Returns:
the number of fields in the physical line.

iterator

public java.util.Iterator iterator()
Return an iterator on this format's LineFormat.FieldInfo set.

Returns:
an iterator on this format's LineFormat.FieldInfo set.

makeConstantImage

public static java.lang.String makeConstantImage(java.lang.String s)
Return a string constant translated to the image syntax recognized by this class.

Returns:
the translated constant

Copyright (C) 2003,2004 dr. Cristiano Sadun